/*
 * @Author: 何 海深 813936564@qq.com
 * @Date: 2023-11-30 15:49:42
 * @LastEditors: 何 海深 813936564@qq.com
 * @LastEditTime: 2024-03-27 19:30:44
 * @FilePath: \vue-project\vite.config.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import copy from 'rollup-plugin-copy'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
/* import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'; */
const resolve = (dir: string) => path.join(__dirname, dir)
// https://vitejs.dev/config/
export default ({ mode }: any) => {
  const env = loadEnv(mode, process.cwd(), '')
  return defineConfig({
    plugins: [
      vue(),
      vueJsx(),
      /* Components({
        resolvers: [AntDesignVueResolver()],
      }), */
      copy({
        targets: [
          {
            src: 'node_modules/@liveqing/liveplayer-v3/dist/component/liveplayer-lib.min.js',
            dest: 'public/js'
          }
        ]
      })
    ],
    define: {
      'process.env': {
        NODE_ENV: JSON.stringify(env.NODE_ENV)
      }
    },
    base: mode == 'development' ? '' : './',
    build: {
      outDir: 'fgw-qkj-one-map',
      target: 'esnext',
      chunkSizeWarningLimit: 1500,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString()
            }
          },
          chunkFileNames: (chunkInfo) => {
            const facadeModuleId = chunkInfo.facadeModuleId
              ? chunkInfo.facadeModuleId.split('/')
              : []
            const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'
            return `js/${fileName}/[name].[hash].js`
          }
        }
      }
    },
    css: {
      //css预处理
      preprocessorOptions: {
        scss: {
          //引入var.scss全局预定义变量
          // 如果报错__dirname找不到，需要安装node,执行yarn add @types/node --save-dev
          additionalData: '@import "./src/assets/scss/common.scss";'
        }
      }
    },
    resolve: {
      alias: {
        // '@':'绝对路径', 若有需要，自行配置其他
        '@': resolve('src')
        //'@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      // hmr: true,
      open: false, //启动项目后打开浏览器
      // port: 8000, //端口
      // host: "0.0.0.0",
      proxy: {
        /*  "/socket.io": {
          target: "http://130.120.3.108",
          ws: true,
          changeOrigin: true,
        }, */
        '/apply': {
          target: 'http://130.120.3.108:8000',
          changeOrigin: true
          /* rewrite: (path) => path.replace(/^\/pymbp/, ""), */
        },
        '/system': {
          target: 'http://130.120.3.108:8000',
          changeOrigin: true
          /* rewrite: (path) => path.replace(/^\/pymbp/, ""), */
        },
        '/openUrl': {
          target: 'http://59.42.210.228:83',
          changeOrigin: true
          /* rewrite: (path) => path.replace(/^\/pymbp/, ""), */
        }
      }
    }
  })
}
